package com.sanley.coronavirus.controller;

import com.github.pagehelper.PageInfo;
import com.sanley.coronavirus.bo.UserBO;
import com.sanley.coronavirus.bo.UserInfoBO;
import com.sanley.coronavirus.common.api.CommonResult;
import com.sanley.coronavirus.entity.User;
import com.sanley.coronavirus.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.security.Principal;
import java.util.List;

@Api(value = "UserController", description = "用户管理")
@Controller
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation("登陆")
    @ResponseBody
    @PostMapping("/login")
    public CommonResult login() {
        System.out.println("login");
        return CommonResult.success("成功");
    }

    @ApiOperation("注册")
    @ResponseBody
    @PostMapping("/register")
    public CommonResult register() {
        return CommonResult.success("成功");
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    @ResponseBody
    public CommonResult getUerInfo(Principal principal) {
        String username = principal.getName();
        User user = userService.getUserInfo(username);
        UserInfoBO userInfoBO = new UserInfoBO();
        BeanUtils.copyProperties(user, userInfoBO);
        return CommonResult.success(userInfoBO);
    }

    @ApiOperation("退出")
    @ResponseBody
    @GetMapping("/logoutProcess")
    public CommonResult logout(Principal principal) {
        if (userService.logout(principal.getName())) {
            return CommonResult.success(null, "退出成功");
        } else {
            return CommonResult.failed("退出失败");
        }
    }

    @ApiOperation("获取所有用户")
    @GetMapping(value = "/manager/list")
    @ResponseBody
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") int page, @RequestParam(name = "size", defaultValue = "5") int size, @RequestParam(value = "keyword", required = false) String keyword) {
        if (keyword != null) {
            keyword = keyword.trim();
            if (keyword.length() == 0) {
                keyword = null;
            }
        }
        List<User> users = userService.findAll(page, size, keyword);
        PageInfo<User> pageInfo = new PageInfo(users);
        return CommonResult.success(pageInfo);
    }


    //添加管理员功能
    @ApiOperation("添加管理员功能")
    @PostMapping(value = "/manager/add")
    @ResponseBody
    public CommonResult add(@RequestParam("username") String username, @RequestParam("name") String name,
                            @RequestParam("password") String password, @RequestParam("unit") String unit,
                            @RequestParam("phone") BigInteger phone, @RequestParam("supermanager") char supermanager) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //加密密码
        String pwd = bCryptPasswordEncoder.encode(password);
        User user = new User().setName(name).setPassword(pwd).setUsername(username).setPhone(phone).setUnit(unit);
        //添加用户
        userService.addUser(user);
        //找到id
        int id = userService.findByUsername(user.getUsername()).getId();
        //添加普通权限
        userService.addUserRole(id);
        //添加超级管理员权限
        if ("是".equals(String.valueOf(supermanager))) {
            userService.addAdminRole(id);
        }
        return CommonResult.success(null, "添加成功");
    }


    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    @ResponseBody
    public CommonResult addUser(@RequestBody UserBO userBO) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        userBO.setPassword(bCryptPasswordEncoder.encode(userBO.getPassword()));
        Boolean flag = userService.addOneUser(userBO);
        if (!flag) {
            return CommonResult.failed("用户名已被注册");
        }
        return CommonResult.success(null, "注册成功");
    }

    //升级权限
    @ApiOperation("升级权限")
    @PostMapping("/manager/toSuper")
    @ResponseBody
    public CommonResult toSuper(int id) {
        userService.addAdminRole(id);
        return CommonResult.success(null);
    }

    //删除用户
    @ApiOperation("删除用户")
    @DeleteMapping("/manager/delete")
    @ResponseBody
    public CommonResult delete(int id) {
        userService.deleteUser(id);
        return CommonResult.success(null);
    }

}
